{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "\n",
    "sys.path.append('../utils')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from aoc import * # get_input, d4, d8, d4n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "YEAR = 2021\n",
    "DAY = 21"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "sample = '''Player 1 starting position: 4\n",
    "Player 2 starting position: 8'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "inp = get_input(YEAR, DAY)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def do_transform(inp):\n",
    "    return list(\n",
    "        map(lambda x: int(x.split(': ')[1]), \n",
    "            filter(lambda x: len(x) > 0, inp.split('\\n'))\n",
    "        ))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "sample = do_transform(sample)\n",
    "inp = do_transform(inp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def part1(inp):\n",
    "    class Die:\n",
    "        def __init__(self):\n",
    "            self.value = 1\n",
    "            self.rolls = 0\n",
    "            \n",
    "        def roll(self, times):\n",
    "            ans = 0\n",
    "            \n",
    "            for _ in range(times):\n",
    "                ans += self.value\n",
    "                self.value += 1\n",
    "                self.rolls += 1\n",
    "                if self.value > 100:\n",
    "                    self.value = 1\n",
    "                    \n",
    "            return ans\n",
    "        \n",
    "    \n",
    "    a, b = inp\n",
    "    pa, pb = 0, 0\n",
    "    die = Die()\n",
    "        \n",
    "    while pa < 1000 and pb < 1000:        \n",
    "        a = (a + die.roll(3) - 1) % 10 + 1\n",
    "        pa += a\n",
    "        \n",
    "        if pa >= 1000:\n",
    "            break\n",
    "            \n",
    "        b = (b + die.roll(3) - 1) % 10 + 1\n",
    "        pb += b\n",
    "        \n",
    "    return min(pa, pb) * die.rolls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "739785"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "part1(sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1006866"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "part1_ans = part1(inp)\n",
    "part1_ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "submit_answer(part1_ans, YEAR, DAY)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def part2(inp):\n",
    "    a, b = inp\n",
    "    \n",
    "    dp = np.zeros(22 * 22 * 11 * 11 * 2, dtype=np.int64).reshape(22, 22, 11, 11, 2)\n",
    "    dp[0, 0, a, b, 0] = 1\n",
    "    coeff = [0, 0, 0, 1, 3, 6, 7, 6, 3, 1]\n",
    "    \n",
    "    for pa in range(21):\n",
    "        for pb in range(21):\n",
    "            for q in range(2):\n",
    "                for a in range(1, 11):\n",
    "                    for b in range(1, 11):\n",
    "                        if dp[pa, pb, a, b, q] == 0:\n",
    "                            continue\n",
    "                            \n",
    "                        if q == 0:\n",
    "                            for die in range(3, 10):\n",
    "                                a1 = (a + die - 1) % 10 + 1\n",
    "                                pa1 = min(21, pa + a1)\n",
    "                                dp[pa1, pb, a1, b, 1 - q] += coeff[die] * dp[pa, pb, a, b, q]\n",
    "                        else:\n",
    "                            for die in range(3, 10):\n",
    "                                b1 = (b + die - 1) % 10 + 1\n",
    "                                pb1 = min(21, pb + b1)\n",
    "                                dp[pa, pb1, a, b1, 1 - q] += coeff[die] * dp[pa, pb, a, b, q]\n",
    "                          \n",
    "    awin = np.sum(dp[21, :, :, :, :])\n",
    "    bwin = np.sum(dp[:, 21, :, :, :])\n",
    "    \n",
    "    return max(awin, bwin)           "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "444356092776315"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "part2(sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "273042027784929"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "part2_ans = part2(inp)\n",
    "part2_ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "submit_answer(part2_ans, YEAR, DAY, level=2)"
   ]
  }
 ],
 "metadata": {
  "@webio": {
   "lastCommId": null,
   "lastKernelId": null
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
